package com.java.simple.study.juc.p7;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhoujunlin
 * @date 2022年08月06日 19:05
 * @desc
 */
@Slf4j(topic = "c.TestStarvation")
public class TestStarvation {

    private static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
    private static final Random RANDOM = new Random();

    private static String cooking() {
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }

    public static void main(String[] args) {
        ExecutorService waiterService = Executors.newFixedThreadPool(2);
        ExecutorService cookerService = Executors.newFixedThreadPool(2);

        /*点餐之后上菜 */
        waiterService.execute(() -> {
            log.debug("处理点餐...");
            Future<String> future = cookerService.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜：{}", future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }

        });

        waiterService.execute(() -> {
            log.debug("处理点餐...");
            Future<String> future = cookerService.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜：{}", future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }

        });

    }

}
